﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data.Common;
using Corona.TagToolkit.Exception;
namespace Corona.TagToolkit
{
    public class PrepareInfo<T> : Corona.TagToolkit.IWeakLink
    {
        internal static string configFile = @"E:\Corona.TagAnyWhere\Corona.TagToolkit\App.config.xml";
        private WeightVector weaklink;
        
        public WeightVector Weaklink
        {
            get { return weaklink; }
            set { weaklink = value; }
        }


        public AppSupport<T>.STConverter stconverter;

        public DbConnection connection;
        public string serverAddress = "";
        public string connectString;
        public string dbProviderName;
        public double relevance;
        public Verify verify;

        public PrepareInfo(AppSupport<T>.STConverter stconverter)
        {
            weaklink = new WeightVector();

            this.stconverter = stconverter;

            XmlDocument config = new XmlDocument();

            try
            {
                config.Load(configFile);
            }
            catch (System.Exception e)
            {
                throw new ConfigureReadFailedException(e.Message);
            }



            XmlNode root = config.SelectSingleNode("configuration");

            verify = new Verify();

            foreach (XmlNode node in root)
            {
                switch (node.Name)
                {
                    case "ServerAddress":
                        serverAddress = node.InnerText;
                        break;
                    case "LocalDataBaseConnectionString":
                        ReadDataBaseConnectionString(node);
                        break;
                    case "AppRegister":
                        ReadAppRegister(node);
                        break;
                    default:
                        break;
                }
            }

        }



        /// <summary>
        /// 创建工具包支持
        /// </summary>
        /// <returns></returns>
        public AppSupport<T> createAppSupport()
        {
            AppSupport<T> appSupport = new AppSupport<T>();

            appSupport.initServer(serverAddress);

            appSupport.initAppSupport(weaklink, relevance,stconverter, connectString, dbProviderName, verify);

            return appSupport;

        }


        private void ReadAppRegister(XmlNode node)
        {
            XmlDocument config = new XmlDocument();

            weaklink = new WeightVector();

            try
            {
                config.Load(node.InnerText.Trim());


                verify.AppDescription = config;


             
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(config.NameTable);
                nsmgr.AddNamespace("corona", "http://www.CoronaTagAnyWhere.com");

                
                XmlNode root = config.SelectSingleNode("/corona:AppRegister", nsmgr);
                if (root == null)
                    throw new System.Exception("请检查您的AppRegister文件是否存在或是否符合规范");
                verify.AppName = root.Attributes["Name"].Value;
                

                root = config.SelectSingleNode("/corona:AppRegister/corona:WeakLink", nsmgr);

                if (root == null)
                    throw new System.Exception("请检查您的AppRegister文件是否存在或是否符合规范");

                foreach (XmlNode innerNode in root)
                {
                    if (innerNode.Name == "Kind")
                    {
                        long weight=0;
                        if (innerNode.Attributes["weight"] != null)
                            weight = long.Parse(innerNode.Attributes["weight"].Value);
                        weaklink.Add(innerNode.Attributes["name"].Value, weight);
                    }
                    else if (innerNode.Name == "Relevance")
                        relevance = double.Parse(innerNode.Attributes["value"].Value);
                }
            }
            catch (System.Exception e)
            {
                throw new ConfigureReadFailedException("未读到AppRegister信息:" + e.Message);
            }
        }
        private void ReadDataBaseConnectionString(XmlNode node)
        {
            foreach (XmlNode innerNode in node.ChildNodes)
            {
                if (innerNode.Name == "providerName")
                    dbProviderName = innerNode.InnerText;
                if (innerNode.Name == "connectionString")
                {
                    string provider="";
                    string source="";
                    foreach (XmlNode connect in innerNode.ChildNodes)
                    {
                        if (connect.Name == "Provider")
                            provider = connect.InnerText.Trim();
                        else if (connect.Name == "Source")
                            source = connect.InnerText.Trim();
                    }
                    connectString = "Provider=" + provider + ";" + "Data Source=" + source;
                }
            }
            if (dbProviderName == null || connectString == null)
                throw new ConfigureReadFailedException("未读到数据库连接信息:dbProviderName = " + dbProviderName + "connectString = " + connectString);
        }
    }
}
